<?php

namespace app\admin\controller\business;

use app\common\controller\Backend;

class Source extends Backend
{
    public function __construct()
    {
        parent::__construct();

        //引入模型
        $this->model = model('Business.Source');
    }

    public function index()
    {
        //对请求中所有的参数设置一个过滤，去掉参数中所有的html标签部分
        $this->request->filter(['strip_tags', 'trim']);

        if($this->request->isAjax())
        {
            //Backend 后台基类控制器中提供的一个获取查询条件的参数方法
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            //查询出总条数 和 每页的数据量
            $total = $this->model
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->count();  //查询总数

            $list = $this->model
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();  //查询数据

            //返回的数据 下方的这两个参数名字是固定，这个是前台插件(boostrap-table这个插件的固定的)
            $result = ['total' => $total, 'rows' => $list];

            //返回json类型
            return json($result);
        }



        return $this->view->fetch();
    }

    public function add()
    {
        if($this->request->isAjax())
        {
            // name="row[name]"
            // name="row[sex]"
            // name="row[age]"
            $params = $this->request->param('row/a'); //以数组的方式存在
            
            //添加
            $result = $this->model->validate('common/Business/Source')->save($params);

            if($result === FALSE)
            {
                $this->error($this->model->getError());
                exit;
            }else
            {
                $this->success();
                exit;
            }
        }

        return $this->view->fetch();
    }

    public function edit($ids = NULL)
    {
        //查询当前编辑的数据是否存在
        $row = $this->model->find($ids);

        if(!$row)
        {
            $this->error(__('No results were found'));
            exit;
        }

        if($this->request->isAjax())
        {
            $params = $this->request->param('row/a');
            $params['id'] = $ids;
            
            //执行更新语句
            $result = $this->model->validate('common/Business/Source')->isUpdate(true)->save($params);

            if($result === FALSE)
            {
                $this->error($this->model->getError());
                exit;
            }else
            {
                $this->success();
                exit;
            }
        }

        $this->assign('row', $row);

        return $this->view->fetch();
    }

    public function del($ids = NULL)
    {
        //查询当前编辑的数据是否存在
        $row = $this->model->select($ids);

        if(!$row)
        {
            $this->error(__('No results were found'));
            exit;
        }

        //执行删除语句
        $result = $this->model->destroy($ids);

        if($result === FALSE)
        {
            $this->error('删除失败');
            exit;
        }else
        {
            $this->success();
            exit;
        }
    }
}
